package zym.diandian.module.popup;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import java.io.File;

import zym.diandian.R;

public class PopView extends View {

    private Bitmap bitmap;

    private boolean isFullScreen = false;

    //是否对图片缩放,默认缩放
    private boolean isImgScale = true;

    private String mAlpha = "";

    private Matrix matrix;

    private int mColor = -2553737;

    private boolean mimg = false;

    private String mImgPath = "";

    private WindowManager mWindowManager;

    private Paint paint;

    private int l = 0;
    private int t = 0;
    private int r = 0;
    private int b = 0;

    private int sw, sh;

    public PopView(Context context) {
        super(context);
    }

    public PopView(Context context, String alpha, int color, String imgpath, boolean isScale, WindowManager windowManager, int sw, int sh) {
        super(context);
        if (isInEditMode()) {
            return;
        }
        this.paint = new Paint();
        this.matrix = new Matrix();

        this.sw = sw;
        this.sh = sh;


        l = 0;
        t = 0;
        r = sw;
        b = sh;

        if (r == 0 && b == 0) {
            r = sw;
            b = sh;
        }
        if (TextUtils.isEmpty(alpha)) {
            this.mAlpha = "100";
        } else {
            this.mAlpha = alpha;
        }

        if (color >= 0) {
            this.mColor = -2553737;
        } else {
            this.mColor = color;
        }

        File file = new File(imgpath);
        if (file.exists()) {
            this.mImgPath = imgpath;
            this.mimg = true;
        } else {
            this.mImgPath = "";
            this.mimg = false;
        }

        this.mWindowManager = windowManager;
        this.isImgScale = isScale;
        this.bitmap = BitmapFactory.decodeFile(this.mImgPath);

        setId(R.id.pop_view);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        this.paint.setAntiAlias(true);

        try {
            this.paint.setColor(mColor);
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.paint.setAlpha(Integer.parseInt(this.mAlpha));
        this.paint.setStyle(Paint.Style.FILL_AND_STROKE);
        if (this.mimg) {

            int bmp_width = this.bitmap.getWidth();
            int bmp_height = this.bitmap.getHeight();

            float width = sw;
            float height = sh;
            float sx = width / bmp_width;
            float sy = height / bmp_height;

            if (this.isImgScale) {
                this.matrix.setScale(sx, sy);
                canvas.drawBitmap(this.bitmap, this.matrix, this.paint);
            } else {
                Rect src = new Rect();// 图片
                Rect dst = new Rect();// 屏幕
                src.left = 0;
                src.top = 0;
                src.right = bmp_width;
                src.bottom = bmp_height;

                dst.left = 0;
                dst.top = 0;
                dst.right = bmp_width;
                dst.bottom = bmp_height;
                canvas.drawBitmap(this.bitmap, src, dst, paint);
            }

        }
        if (this.isFullScreen) {
            this.paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(l, t, r, b, this.paint);
        } else {
            canvas.drawRect(l, t, r, b, this.paint);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public String toString() {
        return "PopView{" +
                "bitmap=" + bitmap +
                ", isFullScreen=" + isFullScreen +
                ", isImgScale=" + isImgScale +
                ", mAlpha='" + mAlpha + '\'' +
                ", matrix=" + matrix +
                ", mColor=" + mColor +
                ", mimg=" + mimg +
                ", mImgPath='" + mImgPath + '\'' +
                ", mWindowManager=" + mWindowManager +
                ", paint=" + paint +
                ", l=" + l +
                ", t=" + t +
                ", r=" + r +
                ", b=" + b +
                '}';
    }
}
